/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic
 */
package com.skt.common_hr.fudraw.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.fudraw.FudrawFacade;
	import com.skt.common_hr.fudraw.model.vo.NoticeDetailVO;
	import com.skt.common_hr.fudraw.model.vo.NoticeFileUpLoadVO;
	import com.skt.common_hr.fudraw.model.vo.fudrawB_VO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.AlertHelper;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class FudrawWriteProxy extends CommonProxy
	{
		public static const NAME:String = "FudrawWriteProxy";

		public var writeKey:String;


		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function FudrawWriteProxy(data:Object=null)
		{
			super(NAME, data);
		}

		public function loadNoticeKey():void
		{
			var params:Object = new Object();
			params["I_NR_NAME"]  = "ZHRBOARD";
			params["I_BOARD_ID"] = "06";
			loadData("Z_HR_0054",params, loadKeyResult, loadKeyFault);
		}


		public function insert(fVo:Object, ac:ArrayCollection, FUNUM:String=''):void
		{
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object = new Object();
			var mode:String;
			if(FUNUM == null || FUNUM == '') {
				mode = 'INS'; 
			}
			else {
				mode = 'MOD';
			}
			params["I_ACTIO"]				= mode;
			params["I_FUNUM"]				= FUNUM;
			
			var itab:Object 		= new Object();
			itab = fVo;
			table.push(itab);
			tables["ITAB"] = table;			
			
			
			
 			var tSub_tabArr:Array = new Array();
	 		for each(var rec:fudrawB_VO in ac)
	 		{
				
	 			var data:Object = new Object;
	 			data.PLA 		= rec.PLA;
	 			data.PFR 		= DateUtil.toBasicString(rec.PFR_DATE);
	 			data.PTO 		= DateUtil.toBasicString(rec.PTO_DATE);
	 			data.PYN 		= rec.PYN;
				
				if(data.PLA != null && data.PLA != '') {
					tSub_tabArr.push(data);	
				}
	 		}			
			
			tables["ITAB1"] = tSub_tabArr;

			saveData("Z_HR_FU03", params, tables, insertResult, insertFault);
		}




		//삭제
		public function deleteData(key:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"]			= Variables.myPernr;
			param["I_FUNUM"]		= key;

			loadData("Z_HR_FU02", param, updateResult, updateFault);
		}








		public function update(record:NoticeDetailVO):void
		{
			var params:Object = new Object();
			params["I_BOARD_ID"]		= "06";
			params["I_CONTENT_IDX"]		= record.CONTENT_IDX;
			params["I_TITLE"]			= record.I_TITLE;
			params["I_CONTENT"]			= record.I_CONTENT;
			params["I_FILE_SIZE"]		= record.I_FILE_SIZE;
			params["I_FILE_NAME"]		= record.I_FILE_NAME;
			params["I_FILE_EXT"]		= record.I_FILE_EXT;
			params["I_FILE_URL"]		= record.I_FILE_URL;
			params["I_BEGDA"]			= record.I_BEGDA;
			params["I_ENDDA"]			= record.I_ENDDA;

			loadData("Z_HR_0038", params, updateResult, updateFault);
		}

		//파일정보업데이트
		public function fileInfoUpdate(record:NoticeDetailVO):void
		{
			CursorManager.setBusyCursor();

			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0038", Variables.serverName);

			var params:Object = new Object();
			params["I_BOARD_ID"]		= record.I_BOARD_ID;
			params["I_CONTENT_IDX"]		= record.CONTENT_IDX;
			params["I_TITLE"]			= record.I_TITLE;
			params["I_CONTENT"]			= record.I_CONTENT;
			params["I_FILE_SIZE"]		= record.I_FILE_SIZE;
			params["I_FILE_NAME"]		= record.I_FILE_NAME;
			params["I_FILE_EXT"]		= record.I_FILE_EXT;
			params["I_FILE_URL"]		= record.I_FILE_URL;

			//loader.load(params, fileInfoResult, fileInfoFault);
		}

		public function fileUpload(fileInfo:NoticeFileUpLoadVO, table:Array):void
		{
			CursorManager.setBusyCursor();

			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0036", Variables.serverName);

			var params:Object = new Object();
			//params["I_CONTENT_IDX"]		= fileInfo.I_CONTENT_IDX.toString();
			params["I_FILE_SIZE"]		= fileInfo.I_FILE_SIZE.toString();
			//params["I_FILE_NAME"]		= fileInfo.I_FILE_NAME;
			params["I_FILE_EXT"]		= fileInfo.I_FILE_EXT;
			//params["I_FILE"]			= fileInfo.I_FILE;
			var tables:Object = new Object();
			tables["BYTE_TAB"] = table;

			//loader.load(params, fileInsertResult, fileInsertFault);
			//loader.save(params, tables, fileUploadResult, fileUploadFault);
		}






















		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function loadKeyResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				writeKey = event.result["O_CONTENT_IDX"];
				facade.sendNotification(FudrawFacade.LOAD_KEY_COMPLETE, this);
			}
			else {
				facade.sendNotification(FudrawFacade.LOAD_KEY_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function loadKeyFault(event:FaultEvent):void
		{
			facade.sendNotification(FudrawFacade.LOAD_KEY_FAILED, this);
			CursorManager.removeBusyCursor();
		}



		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(FudrawFacade.INSERT_NOTICE_COMPLETE_FUDRAW, this);
			}
			else {
				trace( "failed to insert article: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(FudrawFacade.INSERT_NOTICE_FAILED_FUDRAW, this);
			}
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(FudrawFacade.INSERT_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 *
		 * @param
		 * @return void
		 */
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(FudrawFacade.UPDATE_NOTICE_COMPLETE_FUDRAW, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				facade.sendNotification(FudrawFacade.INSERT_NOTICE_FAILED_FUDRAW, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(FudrawFacade.UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}

		public function fileInfoResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				//facade.sendNotification(FudrawFacade.FILEINFO_UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				//facade.sendNotification(FudrawFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 *
		 * @param
		 * @return void
		 */
		private function fileInfoFault(event:FaultEvent):void
		{
			//facade.sendNotification(FudrawFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}







		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
	}
}